clear*
set maxvar 11000

global date _2025_10_28
cd "/Users/atri0032/Dropbox/World Bank/Temperature, poverty, and inequality/Estimations/"

******************************************************************************************
********************************** Table S3: The effects of temperature on inequality
******************************************************************************************

// Subnational level
* FE model 
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 

estimates clear
qui reghdfe gini temp rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
	levelsof code if e(sample), local(count1)
	local Ncountry1 : word count `count1'
	estadd scalar N_country = `Ncountry1'
	levelsof geo_code2_new if e(sample), local(count2)
	local Ncountry2 : word count `count2'
	estadd scalar N_subregion = `Ncountry2'
	quietly summ gini if e(sample)
	estadd sca depmean = r(mean) 	
	est store m1
qui reghdfe theil temp rainfall_sum humid `controls', absorb(geo_code2_new year) cluster(geo_code2_new) keepsingle	
	levelsof code if e(sample), local(count1)
	local Ncountry1 : word count `count1'
	estadd scalar N_country = `Ncountry1'
	levelsof geo_code2_new if e(sample), local(count2)
	local Ncountry2 : word count `count2'
	estadd scalar N_subregion = `Ncountry2'
	quietly summ theil if e(sample)
	estadd sca depmean = r(mean) 	
	est store m2

local opts ///
	cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N N_country N_subregion depmean r2, fmt(0 0 0 3 3) ///
	label("Observations" "No of countries" "No of regions" "R-squared")) ///	
	eqlabels(none) mtitle("Gini" "Theil") ///
	collabels(,none) varlabels(temp "Temperature" rainfall_ave "Rainfall" humid "Humidity") varwidth(25) ///
	title("Table S3 - Subnational - FE")
	
esttab m*, `opts' 
esttab m* using "${date}/_tables/_tab_s3_sub_FE.csv", replace `opts' 

	
* Long difference model
drop sample
local controls ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d   

bysort 	geo_code2_new (year): egen year_min = min(year)
bysort 	geo_code2_new (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort geo_code2_new (year): gen n = _n
bysort geo_code2_new (year): gen N = _N
	keep if N==2
	
encode geo_code2_new, gen(id_region)	
xtset id_region n
foreach v in gini theil temp poor215 poor365 poor685 rainfall_sum humid ln_GDPppp Unempilo agri manuf popgr {
	gen `v'_d = D.`v'
}
	
estimates clear
qui reg gini_d temp_d rainfall_sum_d humid_d `controls', cluster(geo_code2_new) 	
	levelsof code if e(sample), local(count1)
	local Ncountry1 : word count `count1'
	estadd scalar N_country = `Ncountry1'
	levelsof geo_code2_new if e(sample), local(count2)
	local Ncountry2 : word count `count2'
	estadd scalar N_subregion = `Ncountry2'
	est store m1
qui reg theil_d temp_d rainfall_sum_d humid_d `controls', cluster(geo_code2_new) 
	levelsof code if e(sample), local(count1)
	local Ncountry1 : word count `count1'
	estadd scalar N_country = `Ncountry1'
	levelsof geo_code2_new if e(sample), local(count2)
	local Ncountry2 : word count `count2'
	estadd scalar N_subregion = `Ncountry2'
	est store m2

local opts ///
	cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N N_country N_subregion r2, fmt(0 0 0 3) ///
	label("Observations" "No of countries" "No of regions" "R-squared")) ///	
	eqlabels(none) mtitle("Gini" "Theil") ///
	collabels(,none) varlabels(temp "Temperature" rainfall_ave "Rainfall" humid "Humidity") varwidth(25) ///
	title("Table S3 - Subnational - LD")
	
esttab m*, `opts' 
esttab m* using "${date}/_tables/_tab_s3_sub_LD.csv", replace `opts'


//==========================//	
// Table S3 - national level
* FE model 
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 

collapse (mean) gini theil temp rainfall_sum humid `controls', by(code year)

estimates clear
qui reghdfe gini temp rainfall_sum humid `controls', absorb(code year) cluster(code) keepsingle	
	quietly summ gini if e(sample)
	estadd sca depmean = r(mean) 	
	est store m1
qui reghdfe theil temp rainfall_sum humid `controls', absorb(code year) cluster(code) keepsingle	
	quietly summ theil if e(sample)
	estadd sca depmean = r(mean) 	
	est store m2

local opts ///
	cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N depmean r2, fmt(0 3 3) ///
	label("Observations" "R-squared")) ///	
	eqlabels(none) mtitle("Gini" "Theil") ///
	collabels(,none) varlabels(temp "Temperature" rainfall_ave "Rainfall" humid "Humidity") varwidth(25) ///
	title("Table S3 - National - FE")
	
esttab m*, `opts' 
esttab m* using "${date}/_tables/_tab_s3_nat_FE.csv", replace `opts' 

	
* Long difference model
use "${date}/_data/spid_for_analysis_v2.dta", clear

drop sample
local controls ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d   

bysort 	geo_code2_new (year): egen year_min = min(year)
bysort 	geo_code2_new (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort geo_code2_new (year): gen n = _n
bysort geo_code2_new (year): gen N = _N
	keep if N==2
	
encode geo_code2_new, gen(id_region)	
xtset id_region n
foreach v in gini theil temp poor215 poor365 poor685 rainfall_sum humid ln_GDPppp Unempilo agri manuf popgr  {
	gen `v'_d = D.`v'
}
	
collapse (mean) gini_d theil_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d, by(code year)
	
estimates clear
qui reg gini_d temp_d rainfall_sum_d humid_d `controls', cluster(code) 	
	est store m1
qui reg theil_d temp_d rainfall_sum_d humid_d `controls', cluster(code) 
	est store m2

local opts ///
	cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N r2, fmt(0 3) ///
	label("Observations" "R-squared")) ///	
	eqlabels(none) mtitle("$2.15/day" "$3.65/day" "$6.85/day" "Gini" "Theil") ///
	collabels(,none) varlabels(temp "Temperature" rainfall_ave "Rainfall" humid "Humidity") varwidth(25) ///
	title("Table S3 - National - LD")
	
esttab m*, `opts' 
esttab m* using "${date}/_tables/_tab_s3_nat_LD.csv", replace `opts' 

	
//==========================//	
	
// T-test: FE vs LD
* Country-level	
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 

collapse (mean) gini theil temp rainfall_sum humid ln_GDPppp Unempilo agri manuf edupri eduter popgr tech , by(code year)

encode code, gen(id_country)

gen sample = "FE"
	
tempfile fe
save `fe'

use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 
drop sample
bysort 	geo_code2_new (year): egen year_min = min(year)
bysort 	geo_code2_new (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort geo_code2_new (year): gen n = _n
bysort geo_code2_new (year): gen N = _N
	keep if N==2
	
encode geo_code2_new, gen(id_region)	
xtset id_region n
foreach v in gini theil temp poor215 poor365 poor685 rainfall_sum humid ln_GDPppp Unempilo agri manuf popgr  {
	gen `v'_d = D.`v'
}
	
collapse (mean) gini_d theil_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d , by(code year)
	
gen sample = "LD"
	
append using `fe' 

estimates clear
qui reg gini temp rainfall_sum humid i.id_country i.year ln_GDPppp Unempilo agri manuf popgr  if sample =="FE"
	est store gini_fe
qui reg theil temp rainfall_sum humid i.id_country i.year ln_GDPppp Unempilo agri manuf popgr  if sample =="FE"
	est store theil_fe	

qui reg gini_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d if sample =="LD",
	est store gini_ld
qui reg theil_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d if sample =="LD" 
	est store theil_ld

qui suest gini_fe gini_ld	
test [gini_fe_mean]temp - [gini_ld_mean]temp_d = 0
qui suest theil_fe theil_ld	
test [theil_fe_mean]temp - [theil_ld_mean]temp_d = 0

* Subnational-level
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 
drop sample
encode geo_code2_new, gen(id_region)

estimates clear
qui reg gini temp rainfall_sum humid i.id_region i.year ln_GDPppp Unempilo agri manuf popgr 
	est store gini_fe
qui reg theil temp rainfall_sum humid i.id_region i.year ln_GDPppp Unempilo agri manuf popgr 
	est store theil_fe

bysort 	geo_code2_new (year): egen year_min = min(year)
bysort 	geo_code2_new (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort geo_code2_new (year): gen n = _n
bysort geo_code2_new (year): gen N = _N
	keep if N==2
	
xtset id_region n
foreach v in gini theil temp poor215 poor365 poor685 rainfall_sum humid ln_GDPppp Unempilo agri manuf popgr  {
	gen `v'_d = D.`v'
}
	
qui reg gini_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d
	est store gini_ld
qui reg theil_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d
	est store theil_ld

qui suest gini_fe gini_ld	
test [gini_fe_mean]temp - [gini_ld_mean]temp_d = 0
qui suest theil_fe theil_ld	
test [theil_fe_mean]temp - [theil_ld_mean]temp_d = 0


// T-test: country vs subnational
* FE model 
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 
drop sample
encode geo_code2_new, gen(id_region)

gen sample = "sub"
	
tempfile sub
save `sub'
	
collapse (mean) gini theil temp rainfall_sum humid ln_GDPppp Unempilo agri manuf popgr , by(code year)
encode code, gen(id_country)

gen sample = "nat"
	
append using `sub' 

estimates clear
qui reg gini temp rainfall_sum humid i.id_region i.year ln_GDPppp Unempilo agri manuf popgr  if sample =="sub"
	est store gini_sub
qui reg theil temp rainfall_sum humid i.id_region i.year ln_GDPppp Unempilo agri manuf popgr  if sample =="sub"
	est store theil_sub
	
qui reg gini temp rainfall_sum humid i.id_country i.year ln_GDPppp Unempilo agri manuf popgr  if sample =="nat"
	est store gini_nat
qui reg theil temp rainfall_sum humid i.id_country i.year ln_GDPppp Unempilo agri manuf popgr  if sample =="nat"
	est store theil_nat	

qui suest gini_sub gini_nat	
test [gini_sub_mean]temp - [gini_nat_mean]temp = 0
qui suest theil_sub theil_nat	
test [theil_sub_mean]temp - [theil_nat_mean]temp = 0

* LD model 
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 
drop sample
bysort 	geo_code2_new (year): egen year_min = min(year)
bysort 	geo_code2_new (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort geo_code2_new (year): gen n = _n
bysort geo_code2_new (year): gen N = _N
	keep if N==2
	
encode geo_code2_new, gen(id_region)	
xtset id_region n
foreach v in gini theil temp poor215 poor365 poor685 rainfall_sum humid ln_GDPppp Unempilo agri manuf popgr  {
	gen `v'_d = D.`v'
}
	
gen sample = "sub"	
tempfile sub
save `sub'
	
use "${date}/_data/spid_for_analysis_v2.dta", clear

local controls ln_GDPppp Unempilo agri manuf popgr 
drop sample
bysort 	geo_code2_new (year): egen year_min = min(year)
bysort 	geo_code2_new (year): egen year_max = max(year)

keep if year == year_min | year == year_max

bysort geo_code2_new (year): gen n = _n
bysort geo_code2_new (year): gen N = _N
	keep if N==2
	
encode geo_code2_new, gen(id_region)	
xtset id_region n
foreach v in gini theil temp poor215 poor365 poor685 rainfall_sum humid ln_GDPppp Unempilo agri manuf popgr  {
	gen `v'_d = D.`v'
}
	
collapse (mean) gini_d theil_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d, by(code year)

gen sample = "nat"
	
append using `sub' 

estimates clear
qui reg gini_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d if sample =="sub" 	
	est store gini_sub
qui reg theil_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d if sample =="sub"
	est store theil_sub

qui reg gini_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d if sample =="nat" 	
	est store gini_nat
qui reg theil_d temp_d rainfall_sum_d humid_d ln_GDPppp_d Unempilo_d agri_d manuf_d popgr_d if sample =="nat"
	est store theil_nat

qui suest gini_sub gini_nat	
test [gini_sub_mean]temp_d - [gini_nat_mean]temp_d = 0
qui suest theil_sub theil_nat	
test [theil_sub_mean]temp_d - [theil_nat_mean]temp_d = 0
